// ConsoleAppWithWindowText.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

//Function protype
void SetConsoleWindowsText( TCHAR * app);

int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])
{
	//Just instert this at start of main()
	SetConsoleWindowsText(argv[0]);

	//Do something
	 _tprintf(_T("\nHello World\n"));

	//Dummy end of app pause
	char ch[100];
	gets(ch);
    return 0;
}

//
// Set this app's Console Window Text to the app's exe name
//
// From argv[0], the full path to the current app, 
//    get the app .exe name
// Call with argv[0] as the parameter
void SetConsoleWindowsText( TCHAR * app)
{
	//Need to remove folders in path in argv[0]
	//Find first \ from left
	int len = wcslen( app);
	while ((len-->0) && (app[len] != '\\'));
	if (len != -1)
	{
		len +=1;
	}
	else
	{
		len=0;
	}
	//len now points to first char beyond left most \ or to first char if there are no \

	_tprintf(_T("Starting app %s!\n"),app+len);
	_tprintf(_T("\n"));
	
	//Get the Console Window (assumes only one running whose Window Text hasn't been renamed)
	//The following will get any Console window regardless of its text:
	//           HWND hwnd = FindWindow(_T("ConDev_AppCls"),NULL);
	//So assuming all previous console apps have been renamed use the following:
	HWND hwnd = FindWindow(NULL,_T("Console"));
	RETAILMSG(1,(TEXT("Console Window Handle for %s is  %X \n"),app+len,hwnd));
	SetWindowText(hwnd,app+len);
}

